/**
 * @param {WebMail} oWebMail
 * @param {WebMailMailScreen} oScreen
 * @return {WebMailTabs}
 */
function WebMailTabs (oWebMail, oScreen)
{
	this.webmail = oWebMail;
	this.screen = oScreen;
	this.content = $('#right-pane #tabs-pane');
	this.add = $('<div>').pureJSTemplate({ id: 'template-tab-add.html', data : {} });
	this.add.appendTo(this.content);
	this.initLiveActions();
}

WebMailTabs.prototype =
{
	initLiveActions: function ()
	{
		var obj = this;
		$(this.content).delegate('.tab .tab-name', 'click', function () {
			obj.webmail.prepearHash($(this).parent().attr('rel'));
		});

		$(this.content).delegate('.tab .close', 'click', function () {
			$(this).parent().remove();
		});

		$(this.add).delegate('.tab-add', 'click', function () {
			obj.buildTab(obj.getCurrentPane().attr('rel'), obj.getCurrentPane().attr('data-x-tabname'));
		});
	},

	getCurrentPane: function ()
	{
		return this.webmail.screens['mail'].currentPane;
	},

	setActivTab: function (sRelKey)
	{
		$('.tab[rel]', this.content).removeClass('selected');
		var oItem = $('.tab[rel=' + sRelKey + ']', this.content);
		if (oItem && 0 < oItem.length)
		{
			oItem.addClass('selected');
		}
	},

	buildTab: function (sRelKey, sTabName)
	{
		var oItem = $('.tab[rel=' + sRelKey + ']', this.content);
		if (!oItem || 0 === oItem.length)
		{
			$('<div>').addClass('tab').attr('rel', sRelKey).pureJSTemplate({
				id   : 'template-tab.html',
				data : {Rel: sRelKey, Name: sTabName}
			}).appendTo(this.content);
		}

		this.setActivTab(sRelKey);
	}
}